package com.daiziru.wuyanmall.service.impl;

import com.daiziru.wuyanmall.dao.OrdersMapper;
import com.daiziru.wuyanmall.dao.ProductCommentsMapper;
import com.daiziru.wuyanmall.entity.Orders;
import com.daiziru.wuyanmall.entity.ProductComments;
import com.daiziru.wuyanmall.entity.ProductCommentsVO;
import com.daiziru.wuyanmall.service.ProductCommentService;
import com.daiziru.wuyanmall.utils.PageHelper;
import com.daiziru.wuyanmall.vo.ResultStatus;
import com.daiziru.wuyanmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class ProductCommentsServiceImpl implements ProductCommentService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ProductCommentsMapper productCommentsMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public ResultVO ListCommentsByProductId(String productId, int pageNum, int limit) {
        //分页的实现
//        List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommentsById(productId);
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);

        //1、记录的总条数
        int count = productCommentsMapper.selectCountByExample(example);
        //2、计算总页数（需要确定每一页的条数 pageSize = limit)
        int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
        //3、查询当前页面的数据（需要查询用户的用户信息，所以用到连接查询）
        int start = (pageNum - 1) * limit;
        List<ProductCommentsVO> list = productCommentsMapper.selectCommentsById(productId, start, limit);

        ResultVO resultVO = new ResultVO(ResultStatus.OK, "succes", new PageHelper<ProductCommentsVO>(count,pageCount,list));
        return resultVO;
    }

    @Override
    public ResultVO listCommentsCountByProductId(String productId) {
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        //1、查询总评价条数
        int total = productCommentsMapper.selectCountByExample(example);
        criteria.andEqualTo("commType", 1);
        //2、好评数
        int goodTotal = productCommentsMapper.selectCountByExample(example);
        //3、中评数
        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("productId", productId);
        criteria1.andEqualTo("commType", 0);
        int midTotal = productCommentsMapper.selectCountByExample(example1);
        //4、差评数
        int badTotal = total - midTotal - goodTotal;
        //5、好评率
        double percent = ((double)goodTotal/(double)total) * 100;
        String percentStr = (percent + "").substring(0, (percent + "").lastIndexOf('.') + 3) + '%';
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("goodTotal",goodTotal);
        map.put("midTotal",midTotal);
        map.put("badTotal",badTotal);
        map.put("percentStr",percentStr);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", map);
        return resultVO;
    }

    @Override
    public ResultVO addProductComment(ProductComments productComments) {
        productComments.setSepcName(new Date());
        String commId = UUID.randomUUID().toString().replace("-", ""); //设置评论的主键id
        productComments.setCommId(commId);


        int i = productCommentsMapper.insert(productComments);
        if(i > 0) {
            // i > 0 评论成功，然后根据订单id修改订单的状态为已完成(status = 5)
            String orderId = productComments.getOrderItemId(); //订单id
            Orders orders = new Orders();
            orders.setOrderId(orderId);
            orders.setStatus("5");
            int i1 = ordersMapper.updateByPrimaryKeySelective(orders);
            return new ResultVO(ResultStatus.OK,"success",null);
        } else {
            return new ResultVO(ResultStatus.NO,"fail", null);
        }
    }
}
